package com.haiwen.boot.controller;
import com.haiwen.boot.pojo.BlogWithBLOBs;
import com.haiwen.boot.service.BlogContentService;
import com.haiwen.boot.vo.ResultMap;
import com.haiwen.boot.vo.responseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
@CrossOrigin
@RestController
@Api(tags = "博客内容处理")
public class blogContentController {
    //处理图片的上传问题
    @Value("${file.path}")
    private String path;
    @Autowired
    private BlogContentService service;
    @PostMapping(value = "/uploadImage")
    @ApiOperation(value = "对文章编辑器图片的处理")
    public String blogUpload(@RequestParam("image") MultipartFile file,
                             HttpServletRequest request){
        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        String contextPath = request.getServletContext().getContextPath();
        if (!file.isEmpty()){
            //获取文件名
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            String localPath="http://"+"127.0.0.1"+":"+serverPort+contextPath+"/blogimg/"+filename;
            try {
                file.transferTo(new File(path + File.separator +filename));
                return localPath;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }
    //接收md文件请求
    @PostMapping(value = "/uploadText")
    @ApiOperation(value = "后台文章保存")
    public responseData<Object> receiveMdFile(BlogWithBLOBs blog){
        int nums = service.TextUpload(blog);
        if (nums>=1){
            return new responseData<>(200,"保存成功");
        }
            return new responseData<>(500,"失败");
    }
    @PostMapping(value = "/listShow")
    public responseData<BlogWithBLOBs> listShow(){
        List<BlogWithBLOBs> blogWithBLOBs = service.listShow(true);
        return new responseData<>(200,"成功",blogWithBLOBs);
    }
    //删除与发布操作
    @PostMapping(value = "/delAndRelBlog")
    @ApiOperation(value = "删除与发布")
    public ResultMap delAndRelBlog(BlogWithBLOBs blog){
      return service.delAndRelByTitle(blog);
    }
    //响应md文件，展示数据
    @GetMapping(value = "/blogContent")
    @ApiOperation(value = "展示文章内容")
    public responseData<BlogWithBLOBs> blogContent(@RequestParam Integer blId){
        BlogWithBLOBs blog = service.blogShow(blId);
        if (blog!=null){
            return  new responseData<>(200,"成功",blog);
        }
        return  new responseData<>(500,"失败",null);
    }
    //浏览量
    @GetMapping(value = "/reading")
    @ApiOperation(value = "浏览量")
    public String Reading(Integer id,HttpServletRequest request){
        String strBackUrl = "http://" + request.getServerName() //服务器地址
                + ":"
                + request.getServerPort()           //端口号
                + request.getContextPath()      //项目名称
                + request.getServletPath()      //请求页面或其他地址
                + "?" + (request.getQueryString()); //参数
        service.Reading(id);
        return "成功";
    }
}
